package khai.tomcat.http;

import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Request {
    private String uri;
    private Map<String, String> parameterMap = new HashMap<>();

    Request(InputStream is) throws IOException {
        String text = "";
        byte[] buff = new byte[1024];
        int line = 0;
        if ((line = is.read(buff)) > 0) {
            text = new String(buff, 0, line);
        }
        System.out.println(text);

        String[] arr = text.split("\\n");

        //get /one http/1.1
        String uri = arr[0].split("\\s")[1];
        if (uri.contains("?")) {
            //?aaa=1&fasfd=4
            String method = uri.substring(uri.lastIndexOf("?") + 1);
            if (method != null && method.trim() != "") {
                for (String s : method.split("&")) {
                    String[] note = s.split("=");
                    if (note.length == 2) {
                        parameterMap.put(note[0], note[1]);
                    }

                }
            }
        }
        this.uri = uri;
    }

    public String getUri() {
        return this.uri;
    }

    public Map<String, String> getParameterMap() {
        return parameterMap;
    }

    public static void main(String[] args) {
        String a ="GET /one HTTP/1.1";
        System.out.println(JSON.toJSON(a.split("\\s")));
    }
}
